package com.wuzao.controller.advice;

import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

@ControllerAdvice
public class MyControllerAdvice {
    @ExceptionHandler(value = ArithmeticException.class)
    public String exception()
    {
        System.out.println("监控所有Controller，任意Controller产生异常，都会进到这里");
        return "arithmetic";
    }

    @ModelAttribute(value = "message")
    public String modelAttribute()
    {
        return "全局消息，任意Controller都会获取到";
    }

    @InitBinder
    public void timeFormat(WebDataBinder binder) {
        System.out.println("所有的Controller的时间格式化都会进到这里处理");
        binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }
}
